/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

import blueprint4j.utils.*;

public class DataPairFieldSetCombiner extends DataPairDefault implements FieldBinding,ActionListener {
    
    private JComboBox type=null;
    private JPanel main_panel = new JPanel();
    private Binder binder = new Binder();
    private VectorDataPair vdata_pair = new VectorDataPair();
    private boolean editable = false;
    private BindSetCombinerInterface field_set = null;
    
    public DataPairFieldSetCombiner(Binder binder,BindSetCombinerInterface p_field)
    throws BindException {
        this(binder,p_field, true);
    }
    
    public DataPairFieldSetCombiner(Binder binder,BindSetCombinerInterface p_field, boolean editable) {
        super(binder,p_field);
        this.editable = editable;
        field_set = p_field;
        setLabel(new JLabel(field_set.getDescription()));
        type = new JComboBox(field_set.getTypes());
        type.addActionListener(this);
        rebuild();
        setComponent(main_panel);
    }
    
    private void rebuild()  {
        try {
            type.setSelectedItem(field_set.getTypeAsString());
            vdata_pair.intoBindable();
            field_set.setInto();
            main_panel.removeAll();
            main_panel.add(type);
            binder = new Binder();
            vdata_pair.clear();
            main_panel.add(
                    binder.createDataGroup(
                    vdata_pair,
                    field_set.getStoreForType(),
                    field_set.getDescription()
                    )
                    );
            field_set.reload();
            vdata_pair.intoControl();
            main_panel.repaint();
        } catch (Exception exception) {
            Log.debug.out(exception);
        }
    }
    
    public void actionPerformed(ActionEvent event) {
        try {
            field_set.getType().set((String)type.getSelectedItem());
            rebuild();
        } catch (Exception exception) {
            Log.debug.out(exception);
        }
    }
    
    public boolean isChanged() {
        return vdata_pair.isChanged();
    }
    
    public void intoControl() {
        vdata_pair.intoControl();
    }
    
    public void intoBindable() {
        try {
            vdata_pair.intoBindable();
            field_set.setInto();
        } catch (Exception exception) {
            Log.debug.out( exception);
        }
    }
    
    public Bindable getBindable() {
        return field_set.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return field_set;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field_set = (BindSetCombinerInterface)p_field;
    }
    
    public void resetControl() {}
    
}
